/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.squirrelm.my.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 *
 * @author 松鼠M
 */
@Controller
public class TestFetchImageController {

//    @Autowired
//    private DefaultHttpClient  httpClient;


    @RequestMapping(value = "/img.html", method = RequestMethod.GET, produces = "application/octet-stream")
    public void fetchImage(HttpServletResponse response) throws IOException {
        String urlString   = "http://www.baidu.com/img/baidu_sylogo1.gif";
	StringBuilder  buf = new StringBuilder();
	try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.152 Safari/535.19 CoolNovo/2.0.3.55");
            connection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
            connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
            connection.setRequestProperty("UA-CPU", "x86");
            connection.setRequestProperty("Accept-Encoding", "gzip");//为什么没有deflate呢
            connection.setRequestProperty("Content-type", "text/html");
            connection.setRequestProperty("Connection", "close"); //keep-Alive，有什么用呢，你不是在访问网站，你是在采集。嘿嘿。减轻别人的压力，也是减轻自己。
            connection.setUseCaches(false);//不要用cache，用了也没有什么用，因为我们不会经常对一个链接频繁访问。（针对程序）

            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == 200) {
                    InputStream stream = connection.getInputStream();
                    BufferedReader in = new BufferedReader(new InputStreamReader(stream));
                    OutputStream out = response.getOutputStream();
                    IOUtils.copy(stream, out);
//                    String line;
//                    while (null != (line = in.readLine())) {
//                            buf.append(line);
//                    }
//                    byte[]  bytes   = buf.toString().getBytes();
                    response.setContentType("image/gif");
//                    response.setContentLength(bytes.length);

//                    OutputStream    out = null;
//                    try {
//                        out.write(bytes);
//                    } finally {
//                        if (out != null)
//                        {
//                            try {
//                                out.close();
//                            } catch (IOException ignore) {}
//                        }
//                    }



                    stream.close();
                    in.close();
                    connection.disconnect();
            }
	} catch (Exception e) {
		e.printStackTrace();
	}

//	System.out.println("aaa");
    }
}
